package com.cryptooj.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cryptooj.project.common.BaseResponse;
import com.cryptooj.project.common.ErrorCode;
import com.cryptooj.project.common.ResultUtils;
import com.cryptooj.project.exception.BusinessException;
import com.cryptooj.project.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.cryptooj.project.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.cryptooj.project.model.entity.QuestionSubmit;
import com.cryptooj.project.model.entity.User;
import com.cryptooj.project.model.vo.QuestionSubmitVO;
import com.cryptooj.project.service.QuestionSubmitService;
import com.cryptooj.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *  题目作答提交controller
 * */
@RestController
@RequestMapping("/question_submit")
@Slf4j
//@Deprecated // 可以将QuestionSubmitController的接口方法直接放到QuestionController
public class QuestionSubmitController {
    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    /**
     *  提交题目
     * */
    @PostMapping("/do")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest, HttpServletRequest request){
        if(questionSubmitAddRequest == null|| questionSubmitAddRequest.getQuestionId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        final User loginUser = userService.getLoginUser(request);
        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(questionSubmitId);
    }

    /**
     *  获取分页后的题目作答提交列表
     *  (除了管理员外，普通用户仅能看到脱敏后的作答提交列表）
     * */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitVOPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest, HttpServletRequest request){
        long current = questionSubmitQueryRequest.getCurrent();
        long pageSize = questionSubmitQueryRequest.getPageSize();
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, pageSize), questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        final User loginUser = userService.getLoginUser(request);
        Page<QuestionSubmitVO> questionSubmitVOPage = questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser);
        return ResultUtils.success(questionSubmitVOPage);
    }

    /**
     *  获取用户自己的分页后的题目作答提交VO列表
     * */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<QuestionSubmitVO>> listMyQuestionSubmitVOPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest, HttpServletRequest request){
        long current = questionSubmitQueryRequest.getCurrent();
        long pageSize = questionSubmitQueryRequest.getPageSize();
        final User loginUser = userService.getLoginUser(request);
        final Long userId = loginUser.getId();
        questionSubmitQueryRequest.setUserId(userId);
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, pageSize),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        Page<QuestionSubmitVO> questionSubmitVOPage = questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser);
        return ResultUtils.success(questionSubmitVOPage);
    }
}
